package io.itit.ecp.admin.server.service.impl;

import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import io.itit.ecp.admin.common.enums.IMeterDeviceEnums;
import io.itit.ecp.admin.server.entity.*;
import io.itit.ecp.admin.server.mapper.SceneRoomMapper;
import io.itit.ecp.admin.server.service.*;
import io.itit.grass.common.exception.GrassException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *
 * @Author：Roy
 * @Description：空间服务实现类
 * @Date：Create by 2023-11-23 10:29:25
 */
@Service
public class SceneRoomServiceImpl extends JoinServiceImpl<SceneRoomMapper, SceneRoomEntity> implements ISceneRoomService {
    @Resource
    private IMeterDeviceService meterDeviceService;
    @Resource
    private IDeviceService deviceService;
    @Resource
    private IEarlyWarningService earlyWarningService;
    @Resource
    private IEnergyDataService energyDataService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCompanyId(String companyId, List<String> ids){
//        GrassException.throwEx(ids.isEmpty(), "空间ID不能为空！");
        if (ids == null || ids.size() == 0){
            return true;
        }
        return this.lambdaUpdate()
                .set(SceneRoomEntity::getSceneCompanyId, companyId)
                .in(SceneRoomEntity::getId, ids)
                .update();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeRooms(List<String> ids) {
        // 查询关联的计量设备.
        long countMeterDevice = meterDeviceService.lambdaQuery()
                .select(MeterDeviceEntity::getId)
                .in(MeterDeviceEntity::getSceneRoomId,ids)
                .eq(MeterDeviceEntity::getDelFlag, IMeterDeviceEnums.DelFlag.delFlag1.delFlag)
                .count();
        GrassException.throwEx(countMeterDevice > 0,"项目已被计量设备模块关联，请在解绑后重试！");
        // 查询关联的设备
        long countDevice = deviceService.lambdaQuery()
                .select(DeviceEntity::getId)
                .in(DeviceEntity::getSceneRoomId,ids)
                .count();
        GrassException.throwEx(countDevice > 0,"项目已被设备模块关联，请在解绑后重试！");
        // 查询关联的预警设置
        long countEarlyWarning = earlyWarningService.lambdaQuery()
                .select(EarlyWarningEntity::getId)
                .in(EarlyWarningEntity::getSceneRoomId,ids)
                .count();
        GrassException.throwEx(countEarlyWarning > 0,"项目已被预警设置模块关联，请在解绑后重试！");
        // 查询关联的能源数据
        long countEnergyData = energyDataService.lambdaQuery()
                .select(EnergyDataEntity::getId)
                .in(EnergyDataEntity::getSceneRoomId,ids)
                .count();
        GrassException.throwEx(countEnergyData > 0,"项目已被能源数据模块关联，请在解绑后重试！");
        // 删除空间
        return this.removeBatchByIds(ids, 1000);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveRoomBatch(List<SceneRoomEntity> entities) {
        return this.saveBatch(entities);
    }

}
